﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Protocol;
using System.Diagnostics.Contracts;

namespace MDS
{
    public class StockHelper
    {
        /// <summary>
        /// 计算涨停价
        /// </summary>
        /// <param name="basicInfo"></param>
        public static void CalculateMaxAndMinOrderPrice(GWMKTBasicInfoANS basicInfo)
        {
            if (basicInfo.Closeprice != null)
            {
                if (basicInfo.StkName.Value.Contains("ST")
                    || basicInfo.SuspendedFlag == SuspendedFlag.True)
                {
                    basicInfo.MaxOrderPrice = basicInfo.Closeprice * (1.05);
                    basicInfo.MinOrderPrice = basicInfo.Closeprice * (0.95);
                }
                else
                {
                    basicInfo.MaxOrderPrice = basicInfo.Closeprice * (1.1);
                    basicInfo.MinOrderPrice = basicInfo.Closeprice * (0.9);
                }
            }

            if (basicInfo.TradeStatus != null && basicInfo.TradeStatus == TradeStatus.Y)
            {
                basicInfo.MaxOrderPrice = 99999.999;
                basicInfo.MinOrderPrice = 0;
            }

            if (basicInfo.Decimal != null && basicInfo.Decimal > 0)
            {
                if (basicInfo.MaxOrderPrice != 99999.99)
                {
                    basicInfo.MaxOrderPrice = StockAmount.Round(basicInfo.MaxOrderPrice.Value, basicInfo.Decimal);
                    basicInfo.MinOrderPrice = StockAmount.Round(basicInfo.MinOrderPrice.Value, basicInfo.Decimal);
                }
                else
                {
                    if (basicInfo.Decimal == 2)
                    {
                        basicInfo.MaxOrderPrice = 99999.99;
                    }
                }
            }
        }


        /// <summary>
        /// 格式化基础行情的数量信息
        /// </summary>
        /// <param name="basicInfo"></param>
        static public void FormatAmount(GWMKTBasicInfoANS basicInfo)
        {

            if (basicInfo.convertQty != null)
            {
                if (basicInfo.TotalCirculatingShare != null)
                {
                    basicInfo.TotalCirculatingShare = basicInfo.TotalCirculatingShare.Value * basicInfo.convertQty.Value;
                }
                if (basicInfo.TotalIssueQty != null)
                {
                    basicInfo.TotalIssueQty = basicInfo.TotalIssueQty.Value * basicInfo.convertQty.Value;
                }
                if (basicInfo.PreOpenPosition != null)
                {
                    basicInfo.PreOpenPosition = basicInfo.PreOpenPosition.Value * basicInfo.convertQty.Value;
                }
                if (basicInfo.OpenPosition != null)
                {
                    basicInfo.OpenPosition = basicInfo.OpenPosition.Value * basicInfo.convertQty.Value;
                }
                if (basicInfo.PrevTotalMktKnockQty != null)
                {
                    basicInfo.PrevTotalMktKnockQty = basicInfo.PrevTotalMktKnockQty.Value * basicInfo.convertQty.Value;
                }
                if (basicInfo.LowBuyQtyLimit != null)
                {
                    basicInfo.LowBuyQtyLimit = basicInfo.LowBuyQtyLimit.Value * basicInfo.convertQty.Value;
                }
                if (basicInfo.LowSellQtyLimit != null)
                {
                    basicInfo.LowSellQtyLimit = basicInfo.LowSellQtyLimit.Value * basicInfo.convertQty.Value;
                }
                if (basicInfo.HighBuyQtyLimit != null)
                {
                    basicInfo.HighBuyQtyLimit = basicInfo.HighBuyQtyLimit.Value * basicInfo.convertQty.Value;
                }
                if (basicInfo.HighSellQtyLimit != null)
                {
                    basicInfo.HighSellQtyLimit = basicInfo.HighSellQtyLimit.Value * basicInfo.convertQty.Value;
                }
            }

        }


        /// <summary>
        /// 格式化十档行情的数量信息
        /// </summary>
        /// <param name="bidAsk"></param>
        static public void FormatAmount(GWMKTBidAskANS bidAsk, StockTradeRule tradeRule)
        {
            Contract.Requires(bidAsk != null);
            Contract.Requires(tradeRule != null);
            bidAsk.BuyQty1 = bidAsk.BuyQty1 * tradeRule.ConvertQty;
            bidAsk.BuyQty2 = bidAsk.BuyQty2 * tradeRule.ConvertQty;
            bidAsk.BuyQty3 = bidAsk.BuyQty3 * tradeRule.ConvertQty;
            bidAsk.BuyQty4 = bidAsk.BuyQty4 * tradeRule.ConvertQty;
            bidAsk.BuyQty5 = bidAsk.BuyQty5 * tradeRule.ConvertQty;
            bidAsk.BuyQty6 = bidAsk.BuyQty6 * tradeRule.ConvertQty;
            bidAsk.BuyQty7 = bidAsk.BuyQty7 * tradeRule.ConvertQty;
            bidAsk.BuyQty8 = bidAsk.BuyQty8 * tradeRule.ConvertQty;
            bidAsk.BuyQty9 = bidAsk.BuyQty9 * tradeRule.ConvertQty;
            bidAsk.BuyQty10 = bidAsk.BuyQty10 * tradeRule.ConvertQty;
            bidAsk.SellQty1 = bidAsk.SellQty1 * tradeRule.ConvertQty;
            bidAsk.SellQty2 = bidAsk.SellQty2 * tradeRule.ConvertQty;
            bidAsk.SellQty3 = bidAsk.SellQty3 * tradeRule.ConvertQty;
            bidAsk.SellQty4 = bidAsk.SellQty4 * tradeRule.ConvertQty;
            bidAsk.SellQty5 = bidAsk.SellQty5 * tradeRule.ConvertQty;
            bidAsk.SellQty6 = bidAsk.SellQty6 * tradeRule.ConvertQty;
            bidAsk.SellQty7 = bidAsk.SellQty7 * tradeRule.ConvertQty;
            bidAsk.SellQty8 = bidAsk.SellQty8 * tradeRule.ConvertQty;
            bidAsk.SellQty9 = bidAsk.SellQty9 * tradeRule.ConvertQty;
            bidAsk.SellQty10 = bidAsk.SellQty10 * tradeRule.ConvertQty;

        }

        //TODO:preTick不为null时TotalBuyQtyDiff的处理
        static public void FormatTickAvgPrice(GWMKTTicksANS tick, StkType stkType, GWMKTBidAskANS bidAsk, StockTradeRule tradeRule,GWMKTTicksANS preTick)
        {
            //总买量
            tick.TotalBuyQty = 
                bidAsk.BuyQty1 
                + bidAsk.BuyQty2 
                + bidAsk.BuyQty3 
                + bidAsk.BuyQty4 
                + bidAsk.BuyQty5;
            tick.TotalBuyQtyDiff = tick.TotalBuyQty;

            if (stkType == StkType.Bond && tradeRule != null)
            {
                tick.TotalBuyQty = tick.TotalBuyQty * tradeRule.ConvertQty;
                tick.TotalBuyQtyDiff = tick.TotalBuyQtyDiff * tradeRule.ConvertQty;
            }

            //加权平均委买价格
            if (tick.TotalBuyQty > 0)
            {
                tick.BuyAvgPrice =
                    (bidAsk.BuyQty1 * bidAsk.BuyPrice1
                    + bidAsk.BuyQty2 * bidAsk.BuyPrice2
                    + bidAsk.BuyQty3 * bidAsk.BuyPrice3
                    + bidAsk.BuyQty4 * bidAsk.BuyPrice4
                    + bidAsk.BuyQty5 * bidAsk.BuyPrice5)
                    / tick.TotalBuyQty;
                tick.BuyAvgPrice = StockAmount.Round(tick.BuyAvgPrice, 4);
            }
            else
            {
                tick.BuyAvgPrice = 0;
            }

            //总卖量
            tick.TotalSellQty = 
                bidAsk.SellQty1 
                + bidAsk.SellQty2 
                + bidAsk.SellQty3 
                + bidAsk.SellQty4 
                + bidAsk.SellQty5;
            tick.TotalSellQtyDiff = tick.TotalSellQty;

            if (stkType == StkType.Bond && tradeRule != null)
            {
                tick.TotalSellQty = tick.TotalSellQty * tradeRule.ConvertQty;
                tick.TotalSellQtyDiff = tick.TotalSellQtyDiff * tradeRule.ConvertQty;
            }

            //加权平均委卖价格
            if (tick.TotalSellQty.Value > (Int64)0)
            {
                tick.SellAvgPrice =
                    (bidAsk.SellQty1 * bidAsk.SellPrice1
                    + bidAsk.SellQty2 * bidAsk.SellPrice2
                    + bidAsk.SellQty3 * bidAsk.SellPrice3
                    + bidAsk.SellQty4 * bidAsk.SellPrice4
                    + bidAsk.SellQty5 * bidAsk.SellPrice5)
                    / tick.TotalSellQty;
                tick.SellAvgPrice = StockAmount.Round(tick.SellAvgPrice.Value, 4);
            }
            else
            {
                tick.SellAvgPrice = 0;
            }
        }
    }
}
